package com.aaa.dao;

import com.aaa.entity.Identity;
import com.aaa.entity.LeadingUser;
import com.aaa.entity.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class LeadingUserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private RowMapper<LeadingUser> rowMapper=new RowMapper<LeadingUser>() {
        public LeadingUser mapRow(ResultSet resultSet, int i) throws SQLException {
            LeadingUser leadingUser=new LeadingUser();
            leadingUser.setLid(resultSet.getLong("lid"));
            leadingUser.setLuname(resultSet.getString("luname"));
            leadingUser.setPassword(resultSet.getString("password"));
            leadingUser.setPhone(resultSet.getString("phone"));
            leadingUser.setPicture(resultSet.getString("picture"));
            leadingUser.setCreate_time(resultSet.getString("create_time"));
            State state=new State();
            state.setSid(resultSet.getLong("sid"));
            state.setSname(resultSet.getString("sname"));
            leadingUser.setState(state);
            return leadingUser;
        }
    };


    public List<LeadingUser> listPage(String keyword,Integer offset,Integer limit) {
        String sql = "select r.lid,r.luname,r.password,r.phone,r.picture,r.create_time,s.sid,s.sname from leading_user r  left join state s on r.id=s.sid where r.luname like ? order by r.create_time desc limit  ?,?";
        List<LeadingUser> result = jdbcTemplate.query(sql, rowMapper, "%" + keyword + "%", offset, limit);
        return result;
    }

    public Integer count(String keyword){
        String sql="select count(lid) as c from leading_user";
        List<Integer> count=jdbcTemplate.query(sql, new RowMapper<Integer>() {
            public Integer mapRow(ResultSet resultSet, int i) throws SQLException {
                return resultSet.getInt("c");
            }
        });
        Integer integer=Integer.valueOf(count.get(0).toString());
        return integer;
    }
    public Integer state(LeadingUser leadingUser,Integer id){
        String sql="update leading_user set id=? where lid=?";
        int count = this.jdbcTemplate.update(sql, id, leadingUser.getLid());
        return count;
    }
}

